<?php

namespace app\forms;

use app\components\helpers\MappingHelper;
use app\models\Menu;

/**
 * 菜单 表单父类
 */
class MenuForm extends Model
{
    public $id;
    public $parentId;
    public $itemId;
    public $name;
    public $icon;
    public $route;
    public $type;
    public $target;
    public $remark;
    public $level;
    public $status;
    public $sort;

    public function rules()
    {
        return [
            ['parentId', 'integer'],
            ['parentId', 'default', 'value' => 0],

            ['itemId', 'integer'],
            ['itemId', 'default', 'value' => 0],

            ['name', 'trim'],
            ['name', 'string', 'length' => [2, 16]],

            ['icon', 'trim'],
            ['icon', 'string', 'max' => 16],
            ['icon', 'default', 'value' => ''],

            ['route', 'trim'],
            ['route', 'string', 'max' => 32],

            ['type', 'integer'],
            ['type', 'in', 'range' => array_column(Menu::getTypeMapping(), 'id')],
            ['type', 'required'],

            ['target', 'integer'],
            ['target', 'in', 'range' => array_column(Menu::getTargetMapping(), 'id')],
            ['target', 'required'],

            ['status', 'integer'],
            ['status', 'in', 'range' => array_column(MappingHelper::statusList(), 'id')],
            ['status', 'default', 'value' => MappingHelper::STATUS_ACTIVE],

            ['level', 'integer'],
            ['level', 'default', 'value' => 1],

            ['remark', 'trim'],
            ['remark', 'string', 'max' => 64],
            ['remark', 'default', 'value' => ''],

            ['sort', 'integer'],
            ['sort', 'default', 'value' => 0],


            [
                'id',
                'required',
                'on' => ['update', 'delete', 'status', 'view']
            ],
            [
                'id',
                'exist',
                'targetClass' => Menu::class,
                'targetAttribute' => 'id',
                'on' => ['update', 'delete', 'status', 'view']
            ],
            /*添加菜单*/
            [
                'route',
                'unique',
                'targetClass' => Menu::class,
                'message' => t('api', '{attribute} already exist'),
                'on' => ['create']
            ],
            /*更新菜单*/
            ['name', 'checkUnique', 'on' => ['update']],

        ];
    }

    public function scenarios()
    {
        return [
            'index' => ['name'],
            'create' => ['parentId', 'itemId', 'name', 'icon', 'route', 'type', 'target', 'status', 'level', 'remark', 'sort'],
            'update' => [
                'id',
                'parentId',
                'itemId',
                'name',
                'icon',
                'route',
                'type',
                'target',
                'status',
                'level',
                'remark',
                'sort'
            ],
            'delete' => ['id'],
            'status' => ['id', 'status'],
            'view' => ['id'],
            'get-item' => ['itemId'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'route' => t('api', 'Route'),
        ];
    }

    public function checkUnique($attribute, $params)
    {
        $exist = Menu::find()->select(['id'])->where([
            'and',
            ['route' => $this->$attribute],
            ['!=', 'id', $this->id]
        ])->one();
        if ($exist) {
            $this->addError($attribute, t('api', '{attribute} already exist'));
        }
    }
}